package Demo1_myBatisFirstExample.controller;

import Demo1_myBatisFirstExample.dao.CustomerMapper;
import Demo1_myBatisFirstExample.dao.EmpDeptDao;
import Demo1_myBatisFirstExample.dao.UserDao;
import Demo1_myBatisFirstExample.vo.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserController {

    public static void userController(SqlSession sqlSession,SqlSession sqlSession1){
        //4.通过sql会话创建UserDao接口的对象
        UserDao userDaoMapper = sqlSession.getMapper(UserDao.class);
        UserDao userDaoMapper1 = sqlSession1.getMapper(UserDao.class);

        //5.通过userDao执行具体的操作
//            userDaoMapper.addUser(new User(2,"徐豪",20));
//            userDaoMapper.addUser(new User(3,"赵圣骏",23));
//            userDaoMapper.addUser(new User(4,"曾庆萱",21));
//            userDaoMapper.addUser(new User(5,"开祥鹏",22));
//            userDaoMapper.addUser(new User(6,"杨绅",23));
//            userDaoMapper.addUser(new User(7,"乔继峰",20));
//            userDaoMapper.addUser(new User(8,"张仁贵",22));
//            userDaoMapper.addUser(new User(9,"于效东",21));
//            userDaoMapper.addUser(new User(10,"马宏泽",20));
//            userDaoMapper.addUser(new User(11,"郭苏林",22));
//            userDaoMapper.addUser(new User(12,"梁鑫",20));
//            userDaoMapper.addUser(new User(13,"李琪",20));

//            List<User> userList = new ArrayList<>();
//            userList.add(new User(14,"张志雪",20));
//            userList.add(new User(15,"李明跃",21));
//            userList.add(new User(16,"于波",21));
//            userList.add(new User(17,"王雪彤",20));
//            userList.add(new User(18,"张钧钠",21));
//            userList.add(new User(19,"tom",22));
//            userList.add(new User(20,"jerry",20));
//
//            userDaoMapper.addMoreUser(userList);

//            User kitty = new User("kitty", 100);
//            userDaoMapper.addUserAutoIncrement(kitty);

//            User GTQ = new User("光头强", 10);
//            userDaoMapper.addUserSimulateAutoIncrement(GTQ);


//            userDaoMapper.modifyUser(new User(3,null,10));

//            userDaoMapper.deleteUser(2);

//          userDaoMapper.selectUser(13);
//          sqlSession.commit();
//          userDaoMapper.selectUser(14);
//          sqlSession.commit();
//          userDaoMapper.selectUser(15);
//          //注意：SQLSession如果不关闭，不会写入二级缓存
//          sqlSession.close();
//
//        System.out.println("===================================");
//
//        userDaoMapper1.selectUser(13);
//        userDaoMapper1.selectUser(14);
//        userDaoMapper1.selectUser(15);

//          User user1 = userDaoMapper1.selectUser(13);
//          System.out.println(user);
//          System.out.println(user1);

//        System.out.println(userDaoMapper.selectAllUserAutoMapping());

//        List<User> userList = userDaoMapper.selectAllUserOrdered("age");
//        System.out.println(userList);

//            System.out.println(userDaoMapper.selectUserByIdAndName(13, "李琪"));
//
//        int[] ids1 = {1,2,3,4,5};
//        int[] ids2 = {6,7,8,9,10};
//        System.out.println(userDaoMapper.selectUserByIdsArr(ids));

//        List<Integer> ids_list = new ArrayList<>();
//        ids_list.add(6);
//        ids_list.add(7);
//        ids_list.add(8);
//        ids_list.add(9);
//        ids_list.add(10);
//        userDaoMapper.selectUserByIdsList(ids_list);

//        Map<String,int[]> ids_map = new HashMap<>();
//        ids_map.put("idsMap1",ids1);
//        ids_map.put("idsMap2",ids2);
//        userDaoMapper.selectUserByIdsMap(ids_map);

//            List<User> users = userDaoMapper.selectAllUser();
//            System.out.println(users);

//            List<Map<String, Object>> moreUser = userDaoMapper.selectMoreUser(1);
//            System.out.println(moreUser);

        userDaoMapper.selectUsersByLike("张");

    }

    public static void empDeptController(SqlSession sqlSession){
        EmpDeptDao mapper = sqlSession.getMapper(EmpDeptDao.class);
//        List<Map<String, Object>> maps = mapper.selectEmpsAndDept();
//        System.out.println(maps);

        System.out.println(mapper.selectDeptEmpsByDeptno(20).getEmps());
    }

    public static void customerController(SqlSession sqlSession){
        CustomerMapper mapper = sqlSession.getMapper(CustomerMapper.class);
//        mapper.insert(new Customer(3,"kitty","vip","119"));

        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andIdGreaterThan(2);
        mapper.selectByExample(customerExample);

    }


    public static void main(String[] args) {

        try {
            //1.通过核心配置文件建立一个连接，生成一个数据流
            InputStream inputStream = Resources.getResourceAsStream("mybatisConfig.xml");

            //2.通过生成的数据流创建一个 sql会话工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            //3.通过 sql会话工厂 创建具体的 sql会话
            SqlSession sqlSession = sqlSessionFactory.openSession(true);

            //创建另外一个一模一样的但是是独立的SqlSession
            SqlSession sqlSession1 = sqlSessionFactory.openSession(true);

//            userController(sqlSession,sqlSession1);

//            empDeptController(sqlSession);

            customerController(sqlSession);


            //6.最后关闭sql会话资源
            sqlSession.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}
